﻿/*============================================================================
  File:    BackupRestore.cs

  Summary: Implements a sample AMO backup and restore utility in C#.

  Date:    June 13, 2005
------------------------------------------------------------------------------
  This file is part of the Microsoft SQL Server Code Samples.

  Copyright (C) Microsoft Corporation.  All rights reserved.

  This source code is intended only as a supplement to Microsoft
  Development Tools and/or on-line documentation.  See these other
  materials for detailed information regarding Microsoft code samples.

  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.
============================================================================*/

using System;
using Microsoft.AnalysisServices;

namespace Microsoft.Samples.SqlServer
{
    class BackupAndRestore
    {
        /// <summary>
        /// Connects to an Analysis Services server, creates a test Database, does backup, restore and then removes it.
        /// </summary>
        static void Main()
        {
            try
            {
                //--------------------------------------------------------------------------------
                // This is the plan of our code:
                //  1. we'll connect to an Analysis Services server
                //  2. we'll create a new Database (empty Database, just for this sample code, at the end we'll remove it)
                //  3. we'll backup the Database (to a file)
                //  4. we'll delete the Database
                //  4. we'll restore the Database (from the backup file)
                //  5. we'll delete the restored Database
                //--------------------------------------------------------------------------------

                string connectionString = "Data Source=localhost";
                Server server = new Server();
                Database database;
                string backupFile = @"\backup_file_generated_by_AMO_sample_code.abf";

                //--------------------------------------------------------------------------------
                // A couple of notes about backup files in general:
                //  1. a backup file path is on the Analysis Services server machine and NOT on the client 
                //     machine (unless they are the same). The Analysis Services server is the one who does
                //     the backup operation, thus the file path will be resolved on his machine.
                //  2. the standard extension for Analysis Services backup files is .abf
                //--------------------------------------------------------------------------------

                //--------------------------------------------------------------------------------
                // STEP 1: connect to Analysis Services server.
                //--------------------------------------------------------------------------------
                server.Connect(connectionString);

                try
                {
                    //--------------------------------------------------------------------------------
                    // STEP 2: create a new Database
                    //--------------------------------------------------------------------------------
                    database = server.Databases.Add(); // the 'Add()' method will generate a unique Name for the new Database
                    database.Update(); // save the new Database to server

                    //--------------------------------------------------------------------------------
                    // STEPS 3 and 4: backup the newly created Database and delete it.
                    //--------------------------------------------------------------------------------
                    try
                    {
                        //--------------------------------------------------------------------------------
                        // The 'Backup' method requires at least the file path. We also specify a second 
                        // parameter 'true' to specify that the backup file will be overwritten if exists.
                        //
                        // WARNING: in this sample code we overwrite the existing backup file for convinience,
                        // in your application this decision needs to be carefully reviewed.
                        //
                        // Note: in case we don't pass the second parameter (or we pass it as 'false') and the
                        // backup file exists, the Analysis Services server returns an error, so the 'Backup'
                        // method will throw an exception.
                        //--------------------------------------------------------------------------------
                        database.Backup(backupFile, true);
                    }
                    finally
                    {
                        database.Drop();
                    }

                    //--------------------------------------------------------------------------------
                    // STEPS 5: restore the Database.
                    //--------------------------------------------------------------------------------
                    server.Restore(backupFile);

                    //--------------------------------------------------------------------------------
                    // STEPS 5: delete the restored Database.
                    //--------------------------------------------------------------------------------
                    server.Refresh(); // we'll get the new list of Databases from server, including the one we just restored
                    server.Databases[database.Name].Drop();
                }
                finally
                {
                    server.Disconnect();
                }
            }
            catch (AmoException e)
            {
                Environment.ExitCode = 1;
                Console.WriteLine(e.ToString());
            }
        }
    }
}
